//创建shm
// 用它(提供数据给Cli去使用)
#include "comm.hpp"

const int flags = IPC_CREAT | IPC_EXCL;

int main()
{
    key_t key = GetKey();
    int shmid = shmget(key, SHM_SIZE, flags | 0666);
    if (shmid < 0)
    {
        cerr << "ser shmge err" << endl;
    }
    cout << "shm creat success,shmid: " << shmid << endl;
    //挂接共享内存   注意这里的强转
    char *str = (char *)shmat(shmid, nullptr, 0);
    if (*((int *)str) == -1)
    {
        cerr << "shat err" << endl;
    }
    cout << "ser shmat success" << endl;

    //提供数据
    str[0]='i';
    str[1]=' ';
    str[2]='a';
    str[3]='m';
    str[4]=' ';
    str[5]='p';
    str[6]='r';
    str[7]='o';
    str[8]='c';
    str[9]='e';
    str[10]='s';
    str[11]='s';
    str[12]=' ';
    str[13]='A';

    int cnt=10;
    while(cnt--)
    {
        sleep(1);
    }
    cout<<"等待 10 s 完成"<<endl;
    shmdt(str);
    cout<<"shmdt shmid:"<<shmid<<endl;
    sleep(5);
    shmctl(shmid,IPC_RMID,nullptr);
    cout<<"rmshm success"<<endl;
    sleep(5);
    return 0;
}